package day_2022_7_to_8.day48;

/**
 * @author haomin
 * @date 2022/08/23 16:15
 **/
public class Day38_1_Num782_MovesToChessboard {
    public static void main(String[] args) {
        int[][] arr = new int[][]{{0,0,1,1},{1,1,0,0},{0,1,0,1},{1,0,1,0}};
        System.out.println(movesToChessboard(arr));
    }
    public static int movesToChessboard(int[][] board) {
        int flag = 0, count = 0;
        for(int i = 0; i < board[0].length-1; ++i){
            if(board[0][i] == board[0][i+1]){
                flag++;
                if(flag == 1){
                    if(i+2 < board[0].length){
                        changeCol(board,i+1,i+2);
                    }else if(i-1 >= 0){
                        changeCol(board,i,i-1);
                    }else return -1;
                    count++;
                }
                if(flag == 2) return -1;
            }else {
                flag = 0;
            }
        }
        flag = 0;
        for(int i = 0; i < board.length-1; ++i){
            if(board[i][0] == board[i+1][0]){
                flag++;
                if(flag == 1){
                    if(i+2 < board.length){
                        changeRow(board,i+1,i+2);
                    }else if(i-1 >= 0){
                        changeRow(board,i,i-1);
                    }else return -1;
                    count++;
                }
                if(flag == 2) return -1;
            }else {
                flag = 0;
            }
        }
        if(judgeBoard(board)){
            return count;
        }else return -1;

    }
    private static boolean judgeBoard(int[][] board){
        for(int i = 0; i < board.length; ++i){
            for(int j = 0; j < board[0].length-1; ++j){
                if(board[i][j] == board[i][j+1] || board[j][i] == board[j+1][i]){
                    return false;
                }
            }
        }
        return true;
    }

    private static void changeCol(int[][] board, int i, int j){
        for(int k = 0; k < board.length; ++k){
            int temp = board[k][i];
            board[k][i] = board[k][j];
            board[k][j] = temp;
        }
    }
    private static void changeRow(int[][] board, int i, int j){
        for(int k = 0; k < board[0].length; ++k){
            int temp = board[i][k];
            board[i][k] = board[j][k];
            board[j][k] = temp;
        }
    }
    class Solution {
        int n = 0, INF = 0x3f3f3f3f;
        int getCnt(int a, int b) {
            return Integer.bitCount(a) != Integer.bitCount(b) ? INF : Integer.bitCount(a ^ b) / 2;
        }
        public int movesToChessboard(int[][] g) {
            n = g.length;
            int r1 = -1, r2 = -1, c1 = -1, c2 = -1, mask = (1 << n) - 1;
            for (int i = 0; i < n; i++) {
                int a = 0, b = 0;
                for (int j = 0; j < n; j++) {
                    if (g[i][j] == 1) a += (1 << j);
                    if (g[j][i] == 1) b += (1 << j);
                }
                if (r1 == -1) r1 = a;
                else if (r2 == -1 && a != r1) r2 = a;
                if (c1 == -1) c1 = b;
                else if (c2 == -1 && b != c1) c2 = b;
                if (a != r1 && a != r2) return -1;
                if (b != c1 && b != c2) return -1;
            }
            if (Integer.bitCount(r1) + Integer.bitCount(r2) != n) return -1;
            if (Integer.bitCount(c1) + Integer.bitCount(c2) != n) return -1;
            if ((r1 ^ r2) != mask || (c1 ^ c2) != mask) return -1;
            int t = 0;
            for (int i = 0; i < n; i += 2) t += (1 << i);
            int ans = Math.min(getCnt(r1, t), getCnt(r2, t)) + Math.min(getCnt(c1, t), getCnt(c2, t));
            return ans >= INF ? -1 : ans;
        }
    }
}